package com.lechuang.taozhequan.view.activity.home;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.lechuang.taozhequan.R;
import com.lechuang.taozhequan.base.BaseActivity;
import com.lechuang.taozhequan.model.bean.HomeSearchResultBean;
import com.lechuang.taozhequan.presenter.CommonAdapter;
import com.lechuang.taozhequan.presenter.net.Netword;
import com.lechuang.taozhequan.presenter.net.ResultBack;
import com.lechuang.taozhequan.presenter.net.netApi.HomeApi;
import com.lechuang.taozhequan.utils.Utils;
import com.lechuang.taozhequan.view.defineView.MGridView;
import com.lechuang.taozhequan.view.defineView.SpannelTextView;
import com.lechuang.taozhequan.view.defineView.ViewHolder;
import com.lechuang.taozhequan.view.defineView.WiperSwitch;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yrj on 2017/8/17.
 * 搜索结果页面
 */


public class SearchResultActivity extends BaseActivity {

    //搜索结果展示的方式  0:按销量展示  1.按好评展示 2.按价格展示  3.按新品展示
    private int showStyle = 0;
    private ImageView iv_price;
    private Context mContext = SearchResultActivity.this;
    //展示在搜索框上的搜索内容
    private String rootName;
    //入参的搜索内容
    private String productName;
    /**
     * 入参的排序方式
     * isVolume 1代表按销量排序从高到底
     * isAppraise 1好评从高到底
     * isPrice  1价格从低到高排序
     * isPrice  2价格从高到低排序
     * isNew    1新品商品冲最近的往后排序
     */
    private String style = "isVolume=1";
    //入参 页数
    private int page = 1;
    //
    //上个界面传递过来的值,用来判断是从分类还是搜索跳过来的 1:分类 2:搜索界面
    private int productstyle = 1;
    //可以刷新的gridview
    private PullToRefreshScrollView refreshScrollView;
    private MGridView gv_search;
    //无网络状态
    private LinearLayout ll_notNet;
    //拼接完的参数
    private String allParameter;
    //刷新重试按钮
    private ImageView iv_tryAgain;
    //商品头图片 淘宝或天猫
    private int headImg;
    //参数map
    private HashMap<String, String> allParamMap;
    private WiperSwitch wiperSwitch;
    //是否人工筛选
    private boolean isPeopleChange = true;

    //保存用户登录信息的sp
    private SharedPreferences se;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //商品集合
        productList = new ArrayList<>();
        allParamMap = new HashMap<>();
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_search_result;
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        se = PreferenceManager.getDefaultSharedPreferences(this);
        ll_notNet = (LinearLayout) findViewById(R.id.ll_noNet);
        //刷新重试
        iv_tryAgain = (ImageView) findViewById(R.id.iv_tryAgain);
        iv_tryAgain.setOnClickListener(this);
        iv_price = (ImageView) findViewById(R.id.iv_price);
        gv_search = (MGridView) findViewById(R.id.gv_search);
        refreshScrollView = (PullToRefreshScrollView) findViewById(R.id.ptrsv);
        refreshScrollView.onRefreshComplete();
        wiperSwitch = (WiperSwitch) findViewById(R.id.wiperSwitch);
        wiperSwitch.setChecked(true);
        //设置监听
        wiperSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                isPeopleChange = checkState;
//                if(isPeopleChange){
//                    findViewById(R.id.ll_type).setVisibility(View.VISIBLE);
//                }else{
//                    findViewById(R.id.ll_type).setVisibility(View.GONE);
//                }
//                getData();
            }
        });
        wiperSwitch.setOnChangedListener(new WiperSwitch.OnChangedListener() {
            @Override
            public void OnChanged(WiperSwitch wiperSwitch, boolean checkState) {
                isPeopleChange = checkState;
                isPeopleChange = checkState;
                if (productList != null) {
                    productList.clear();
                    if (mAdapter != null) {
                        mAdapter.notifyDataSetChanged();
                    }
                }

                if (isPeopleChange) {
                    findViewById(R.id.ll_type).setVisibility(View.VISIBLE);
                } else {
                    findViewById(R.id.ll_type).setVisibility(View.GONE);
                }
                page=1;
                getData();
            }
        });
    }

    @Override
    protected void initData() {
        productstyle = getIntent().getIntExtra("type", 1);
        if (productstyle == 1) {//1:分类
            productName = "&classTypeId=" + getIntent().getStringExtra("rootId");
        } else {  //2 搜索
            productName = "&name=" + getIntent().getStringExtra("rootId");
        }
        rootName = getIntent().getStringExtra("rootName");
        findViewById(R.id.ll_sale).setOnClickListener(this);
        findViewById(R.id.ll_search).setOnClickListener(this);
        findViewById(R.id.ll_like).setOnClickListener(this);
        findViewById(R.id.ll_price).setOnClickListener(this);
        findViewById(R.id.ll_new).setOnClickListener(this);
        ((TextView) findViewById(R.id.tv_search)).setText(rootName);
        refreshScrollView.setOnRefreshListener(refresh);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (null != productList) {
            productList.clear();
        }
        getData();
    }


    private List<HomeSearchResultBean.ProductListBean> productList;
    //适配器
    private CommonAdapter<HomeSearchResultBean.ProductListBean> mAdapter;

    //网络请求获取数据
    private void getData() {
        showWaitDialog("");
        if (allParamMap != null) {
            allParamMap.clear();
        }
        if (Utils.isNetworkAvailable(mContext)) {
            ll_notNet.setVisibility(View.GONE);
            refreshScrollView.setVisibility(View.VISIBLE);
            //区分搜索还是分类跳转
            productstyle = getIntent().getIntExtra("type", 1);
            //拼接之后的参数
            //allParameter = "?page=" + page + productName + "&" + style;
            allParamMap.put("page", page + "");
            if (productstyle == 1) {
                //分类
                allParamMap.put("classTypeId", getIntent().getStringExtra("rootId"));
            } else {
                //搜索
                allParamMap.put("name", getIntent().getStringExtra("rootId"));
            }
            //是否人工筛选
            allParamMap.put("flag", isPeopleChange ? 1 + "" : 0 + "");

            if (showStyle == 0) {
                //按销量
                allParamMap.put("isVolume", 1 + "");
            } else if (showStyle == 1) {
                //按好评排序
                allParamMap.put("isAppraise", 1 + "");
            } else if (showStyle == 2) {
                //按价格排序
                /**
                 * isPrice 1 价格从低到高排序
                 * isPrice 2 价格从高到低排序
                 */
                if (isHighToDown) {
                    //价格从高到低
                    allParamMap.put("isPrice", 2 + "");
                } else {
                    allParamMap.put("isPrice", 1 + "");
                }
            } else if (showStyle == 3) {
                //按新品排序
                allParamMap.put("isNew", 1 + "");
            }
            Netword.getInstance().getApi(HomeApi.class)
                    .searchResult(allParamMap)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new ResultBack<HomeSearchResultBean>(mContext) {
                        @Override
                        public void successed(HomeSearchResultBean result) {
                            hideWaitDialog();
                            if (result == null)
                                return;
                            List<HomeSearchResultBean.ProductListBean> list = result.productList;
                            refreshScrollView.setMode(list.size() > 0 ? PullToRefreshBase.Mode.BOTH : PullToRefreshBase.Mode.PULL_FROM_START);
                            if (productList.size() > 0 && list.toString().equals("[]")) {
                                showShortToast("亲!已经到底了");
                                return;
                            }
                            for (int i = 0; i < list.size(); i++) {
                                productList.add(list.get(i));
                            }
                            refreshScrollView.onRefreshComplete();
                            //只有page=1 的时候设置适配器 下拉刷新直接调用notifyDataSetChanged()
                            if (page == 1) {
                                mAdapter = new CommonAdapter<HomeSearchResultBean.ProductListBean>(mContext, productList, R.layout.item_search_product1) {
                                    @Override
                                    public void setData(ViewHolder viewHolder, Object item) {
                                        HomeSearchResultBean.ProductListBean bean = (HomeSearchResultBean.ProductListBean) item;
                                        viewHolder.displayImage(R.id.iv_img, bean.imgs);
                                        //原件
                                        TextView view = viewHolder.getView(R.id.tv_oldprice);
                                        TextView tv_zhuan = viewHolder.getView(R.id.tv_zhuan);
                                        LinearLayout ll_lingquan = viewHolder.getView(R.id.ll_lingquan);
                                        view.setText("现价¥" + bean.price);
                                        if (bean.zhuanMoney != null&&se.getInt("isAgencyStatus",0)==1)
                                            tv_zhuan.setText(bean.zhuanMoney);

                                        //销量
                                        viewHolder.setText(R.id.tv_number, "已抢" + bean.nowNumber + "件");

                                        //商品名字
                                        if (bean.couponMoney != null&&!"0".equals(bean.couponMoney)) {
                                            viewHolder.setText(R.id.tv_juan, "领券减" + Double.valueOf(bean.couponMoney));
                                            view.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                                        } else {
                                            ll_lingquan.setVisibility(View.GONE);
                                        }
                                      /*  viewHolder.setText(R.id.tv_name, bean.name);*/
                                        //新件
                                        viewHolder.setText(R.id.tv_nowprice, "¥" + bean.preferentialPrice);
                                       /* if (bean.shopType != null && bean.shopType.equals("2")) {
                                            headImg = R.drawable.zhuan_tianmao;
                                        } else {  //shopType 1淘宝 2天猫 默认淘宝
                                            headImg = R.drawable.zhuan_taobao;
                                        }*/

                                        SpannelTextView view1 = viewHolder.getView(R.id.tv_name);
                                        view1.setShopType(bean.shopType);
                                        view1.setDrawText(bean.name);
                                    }
                                };
                                gv_search.setAdapter(mAdapter);
                            } else {
                                mAdapter.notifyDataSetChanged();
                            }
                            gv_search.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                                    startActivity(new Intent(mContext, ProductDetailsActivity.class)
//                                            .putExtra("alipayCouponId", productList.get(position).alipayCouponId)
//                                            .putExtra("alipayItemId", productList.get(position).alipayItemId)
//                                            .putExtra("name",productList.get(position).name)
//                                            .putExtra("price", productList.get(position).price + "")
//                                            .putExtra("preferentialPrice", productList.get(position).preferentialPrice)
//                                            .putExtra("shareIntegral", productList.get(position).shareIntegral)
//                                            .putExtra("shopType", productList.get(position).shopType)
//                                            .putExtra("img", productList.get(position).imgs));
                                    startActivity(new Intent(mContext, ProductDetailsActivity.class)
                                            .putExtra("alipayCouponId", productList.get(position).alipayCouponId)
                                            .putExtra("alipayItemId", productList.get(position).alipayItemId)
                                            .putExtra("name",productList.get(position).name)
                                            .putExtra("price", productList.get(position).price + "")
                                            .putExtra("preferentialPrice", productList.get(position).preferentialPrice)
                                            .putExtra("shareIntegral", productList.get(position).shareIntegral)
                                            .putExtra("shopType", productList.get(position).shopType)
                                            .putExtra("smallImages",productList.get(position).smallImages)
                                            .putExtra("img", productList.get(position).imgs));

                                            /*.putExtra("alipayCouponId", productList.get(position).alipayCouponId)
                                            .putExtra("alipayItemId", productList.get(position).alipayItemId)
                                            .putExtra("nowPrice", productList.get(position).preferentialPrice)
                                            .putExtra("price", productList.get(position).price + ""));
*/
                                }
                            });

                        }
                    });
        } else {
            ll_notNet.setVisibility(View.VISIBLE);
            refreshScrollView.setVisibility(View.GONE);
            hideWaitDialog();
        }

    }


    //价格从高到底
    private boolean isHighToDown = true;

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

            case R.id.ll_search: //搜索
                startActivity(new Intent(mContext, SearchActivity.class));
                finish();
                break;
            case R.id.ll_sale: //按销量排序
                //style = "isVolume=1";
                showStyle = 0;
                selectShowStyle(showStyle);
                page = 1;
                if (productList != null)
                    productList.clear();
                getData();
                break;
            case R.id.ll_like://按好评排序
                //style = "isAppraise=1";
                showStyle = 1;
                selectShowStyle(showStyle);
                page = 1;
                if (productList != null)
                    productList.clear();
                getData();
                break;
            case R.id.ll_price: //按价格排序

                /**
                 * isPrice 1 价格从低到高排序
                 * isPrice 2 价格从高到低排序
                 */
                showStyle = 2;
                selectShowStyle(showStyle);
                if (isHighToDown) {
                    //价格从高到低
                    //style = "isPrice=2";
                    iv_price.setImageResource(R.drawable.shousuohou_jiage_shang);
                    isHighToDown = !isHighToDown;
                    page = 1;
                    if (productList != null)
                        productList.clear();
                    getData();

                } else {
                    // style = "isPrice=1";
                    iv_price.setImageResource(R.drawable.sousuohou_jiage_xia);
                    isHighToDown = !isHighToDown;
                    page = 1;
                    if (productList != null)
                        productList.clear();
                    getData();
                }
                break;
            case R.id.ll_new:  //按新品排序
                //style = "isNew=1";
                showStyle = 3;
                selectShowStyle(showStyle);
                page = 1;
                if (productList != null)
                    productList.clear();
                getData();
                break;

            default:
                break;
        }
    }

    //选择展示的方式
    private void selectShowStyle(int showStyle) {

        if (showStyle == 0) {
            changeStyle(showStyle);
        } else if (showStyle == 1) {
            changeStyle(showStyle);
        } else if (showStyle == 2) {
            changeStyle(showStyle);
        } else if (showStyle == 3) {
            changeStyle(showStyle);
        }
    }


    private void changeStyle(int showStyle) {

        View[] v = {findViewById(R.id.tv_sale), findViewById(R.id.tv_like),
                findViewById(R.id.tv_price), findViewById(R.id.tv_new)};
        View[] v1 = {findViewById(R.id.v_sale), findViewById(R.id.v_like)
                , findViewById(R.id.v_price), findViewById(R.id.v_new)};
        for (int i = 0; i < v.length; i++) {
            ((TextView) v[i]).setTextColor(getResources().getColor(R.color.black));
        }
        for (int i = 0; i < v1.length; i++) {
            v1[i].setVisibility(View.GONE);
        }

        ((TextView) v[showStyle]).setTextColor(getResources().getColor(R.color.rgb_f37c05));
        v1[showStyle].setVisibility(View.VISIBLE);
    }


    private PullToRefreshBase.OnRefreshListener2 refresh = new PullToRefreshBase.OnRefreshListener2() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase refreshView) {
            String label = DateUtils.formatDateTime(
                    mContext.getApplicationContext(),
                    System.currentTimeMillis(),
                    DateUtils.FORMAT_SHOW_TIME
                            | DateUtils.FORMAT_SHOW_DATE
                            | DateUtils.FORMAT_ABBREV_ALL);
            // 显示最后更新的时间
            refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
            page = 1;
            if (null != productList) {
                productList.clear();
            }
            //refreshScrollView.getRefreshableView().smoothScrollTo(0, 0);
            // 模拟加载任务
            getData();
            refreshScrollView.onRefreshComplete();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase refreshView) {
            String label = DateUtils.formatDateTime(
                    mContext.getApplicationContext(),
                    System.currentTimeMillis(),
                    DateUtils.FORMAT_SHOW_TIME
                            | DateUtils.FORMAT_SHOW_DATE
                            | DateUtils.FORMAT_ABBREV_ALL);
            // 显示最后更新的时间
            refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
            page += 1;
            // 模拟加载任务
            getData();
            refreshScrollView.onRefreshComplete();
        }
    };

}
